# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=triton
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
_ver_base=0.9
pkgver=0.9.r4256.b022f317
_commit=b022f3179b3ff6fbafd597099ab0e2f5f07df23a
pkgrel=2
pkgdesc="Triton is a dynamic binary analysis library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/JonathanSalwan/Triton/"
license=('spdx:Apache-2.0')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-capstone"
         #"${MINGW_PACKAGE_PREFIX}-bitwuzla"
         #"${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-z3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-llvm-16"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "git")
source=("${_realname}"::"git+https://github.com/JonathanSalwan/Triton.git#commit=$_commit"
        001-remove-legacy.patch
        002-cmake-install-dir.patch
        003-cmake-target-config.patch)
sha256sums=('c2c5243454084ff80dd3f6840fb9e92f095635a882013017c3323cbd2dd8867d'
            '94100f4106c04360a2d45bbe34a79c8fb909182d5819f9aaa232b9a1ae3cf3d0'
            'b8da26b33d1cf3ff7d620987e45cbef2c32df0b68fe662b0779ffcc933b33ba9'
            '4282cdcc0ec16d1a07857855494ac2fb83b1bbc258323fe1c8b0da25b42c3985')

pkgver() {
  cd "${srcdir}/Triton"
  printf "%s.r%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/Triton"
  patch -p1 -i "${srcdir}"/001-remove-legacy.patch
  patch -p1 -i "${srcdir}"/002-cmake-install-dir.patch
  patch -p1 -i "${srcdir}"/003-cmake-target-config.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX}/opt/llvm-16 \
    -DBUILD_SHARED_LIBS=ON \
    -DBOOST_INTERFACE=ON \
    -DLLVM_INTERFACE=ON \
    -DZ3_INTERFACE=ON \
    -DBITWUZLA_INTERFACE=OFF \
    -DPYTHON_BINDINGS_AUTOCOMPLETE=OFF \
    "${_extra_config[@]}" \
    ../triton

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # support relocation
  local MINGW_PREFIX_W=$(cygpath -m "${MINGW_PREFIX}")
  sed -i "s#${MINGW_PREFIX_W}#\${_IMPORT_PREFIX}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonTargets.cmake
  sed -i "s#${MINGW_PREFIX_W}#\${PACKAGE_PREFIX_DIR}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonConfig.cmake
  sed -i "s#${MINGW_PREFIX}#\${PACKAGE_PREFIX_DIR}#g" "${pkgdir}${MINGW_PREFIX}"/lib/cmake/triton/tritonConfig.cmake

  # License
  install -Dm644 "${srcdir}/Triton/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
